package com.dpiinc.TechPage.room

import androidx.room.TypeConverter
import com.dpiinc.TechPage.constants.FileType
import com.dpiinc.TechPage.model.FileBean
import com.dpiinc.TechPage.model.ImagesBean
import com.dpiinc.TechPage.room.bean.LabelEntity
import com.dpiinc.TechPage.utils.GsonUtil
import com.google.gson.reflect.TypeToken
import java.util.*

/**
 * 数据库类型和复杂类型的转换
 * Created by X on 2018/7/5.
 */
@Suppress("unused")
class AiWriteTypeConverters {
    @TypeConverter
    fun fromTimestamp(value: Long?): Date? {
        return if (value == null) null else Date(value)
    }

    @TypeConverter
    fun dateToTimestamp(date: Date?): Long? {
        return date?.time
    }

    /**
     * 把List<String>转化为String存储
     */
    @TypeConverter
    fun convertStringList(list: List<String>?): String? {
        list ?: return null
        return GsonUtil.toGsonString(list)
    }

    /**
     * 把String还原为List<String>
     */
    @TypeConverter
    fun revertStringList(string: String?): List<String>? {
        string ?: return ArrayList()
        val listType = object : TypeToken<List<String>>() {}.type
        return GsonUtil.stringToBeanList(string, listType)
    }

    /**
     * 把List<LabelEntity>转化为String存储
     */
    @TypeConverter
    fun convertLabelList(list: List<LabelEntity>?): String? {
        list ?: return null
        return GsonUtil.toGsonString(list)
    }

    /**
     * 把String还原为List<LabelEntity>
     */
    @TypeConverter
    fun revertLabelList(string: String?): List<LabelEntity>? {
        string ?: return ArrayList()
        val listType = object : TypeToken<List<LabelEntity>>() {}.type
        return GsonUtil.stringToBeanList(string, listType)
    }

    /**
     * 把List<FileBean>转化为String存储
     */
    @TypeConverter
    fun convertFileList(list: List<FileBean>?): String? {
        list ?: return null
        return GsonUtil.toGsonString(list)
    }

    /**
     * 把String还原为List<FileBean>
     */
    @TypeConverter
    fun revertFileList(string: String?): List<FileBean>? {
        string ?: return ArrayList()
        val listType = object : TypeToken<List<FileBean>>() {}.type
        return GsonUtil.stringToBeanList(string, listType)
    }

    /**
     * 把FileBean转化为String存储
     */
    @TypeConverter
    fun convertFileBean(file: FileBean?): String? {
        file ?: return null
        return GsonUtil.toGsonString(file)
    }

    /**
     * 把String还原为FileBean
     */
    @TypeConverter
    fun revertFileBean(string: String?): FileBean? {
        string ?: return FileBean()
        return GsonUtil.stringToBean(string, FileBean::class.java)
    }

    /**
     * 把ImagesBean转化为String存储
     */
    @TypeConverter
    fun convertImagesBean(file: ImagesBean?): String? {
        file ?: return null
        return GsonUtil.toGsonString(file)
    }

    /**
     * 把String还原为ImagesBean
     */
    @TypeConverter
    fun revertImagesBean(string: String?): ImagesBean? {
        string ?: return ImagesBean()
        return GsonUtil.stringToBean(string, ImagesBean::class.java)
    }

    /**
     * 把FileType转化为String存储
     */
    @TypeConverter
    fun convertFileType(type: FileType?): Int? {
        return type?.value
    }

    /**
     * 把String还原为FileType
     */
    @TypeConverter
    fun revertFileType(value: Int?): FileType? {
        value ?: return FileType.UNKNOWN
        return FileType.parse(value)
    }

}
